package com.sjd.web.api.mall;

import com.alibaba.fastjson.JSONObject;
import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.core.page.TableDataInfo;
import com.sjd.mall.domain.dto.goods.*;
import com.sjd.mall.service.goods.GoodsCommentService;
import com.sjd.mall.service.goods.GoodsService;
import com.sjd.mall.service.goods.GoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author : Mr.s
 * @version : 1.0
 * @description : 微信商品模块
 * @date : 2022-01-26 12:42
 */
@Api(tags = "微信商品模块(小程序)")
@Slf4j
@RestController
@RequestMapping(value = "/weixin/api/goods")
public class WxGoodsApi extends BaseController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private GoodsCommentService goodsCommentService;

    /**
     * 获取banner
     *
     * @return object
     */
    @ApiOperation(value = "获取banner",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = "/banner")
    public TableDataInfo getBanner() {
        List<JSONObject> banner = goodsService.getBanner();
        log.info("获取banner,响应 {}", banner);
        return getDataTable(banner);
    }

    /**
     * 获取所有商品分类
     *
     * @return object
     */
    @ApiOperation(value = "获取所有商品分类",
            response = SjdGoodsTypeQueryResponseDTO.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = "/types")
    public Object getGoodsType(@Valid SjdAppGoodsTypeQueryRequestDTO requestDTO, BindingResult results) {
        if (results.hasErrors()) {
            return AjaxResult.error(Objects.requireNonNull(results.getFieldError()).getDefaultMessage());
        }
        List<SjdAppGoodsTypeQueryResponseDTO> responseDTOList = goodsTypeService.queryTypesApp(requestDTO);
        log.info("获取banner,响应 {}", responseDTOList);
        return getDataTable(responseDTOList);
    }

    /**
     * 获取所有商品根据分类
     *
     * @return object
     */
    @ApiOperation(value = "获取商品列表",
            response = SjdGoodsTypeQueryResponseDTO.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = "/goods-list")
    public TableDataInfo getGoodsList(SjdGetGoodsListRequestDTO requestDTO) {
        startPage();
        List<SjdGetGoodsListResponseDTO> allOlineGoodsList = goodsService.getGoodsList(requestDTO);
        log.info("获取商品列表,响应 {}", allOlineGoodsList);
        return getDataTable(allOlineGoodsList);
    }

    /**
     * 获取商品评论
     *
     * @return object
     */
    @ApiOperation(value = "获取商品评论",
            response = SjdGoodsCommentQueryResponseDTO.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = "/goods-comment")
    public Object getGoodsComment(@Valid SjdGoodsCommentQueryRequestDTO requestDTO, BindingResult results) {
        if (results.hasErrors()) {
            return AjaxResult.error(Objects.requireNonNull(results.getFieldError()).getDefaultMessage());
        }
        startPage();
        List<SjdGoodsCommentQueryResponseDTO> responseDTOList = goodsCommentService.queryCommentApp(requestDTO);
        log.info("获取商品评论,响应 {}", responseDTOList);
        return getDataTable(responseDTOList);
    }

    /**
     * 添加商品评论
     *
     * @return object
     */
    @ApiOperation(value = "添加商品评论",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/goods-comment-add")
    public AjaxResult addGoodsComment(@RequestBody SjdGoodsCommentAddRequestDTO requestDTO) {
        return goodsCommentService.addComment(requestDTO);
    }


}
